﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;

public partial class UserManagement_AddUserList : AdminUserBase
{
    public List<V_user> UserList = new List<V_user>();
    public int CurrentTraingClassId = 0;
    public string CurrentTraingClassName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!string.IsNullOrEmpty(Request.QueryString["TClassId"]))
        {
            CurrentTraingClassId = int.Parse(Request.QueryString["TClassId"].ToString());
            CurrentTraingClassName = Request.QueryString["TClassName"].ToString();
            hfClassId.Value = CurrentTraingClassId.ToString();
        }
        if (!this.IsPostBack)
        {
            SetPageData();
        }
    }

    private void SetPageData()
    {
        
        //}
        TrainClassModule TCM = new TrainClassModule();
        V_TrainClass model = TCM.GetTrainClassModelById(CurrentTraingClassId);
        lblMaxUserCount.Text = model.MaxUserCount.ToString();
        lblVTC_name.Text = model.VTC_name;
        lblVTC_Number.Text = model.VTC_Number;
        lblVTC_starDate.Text = model.VTC_StartDate.ToString("yyyy-MM-dd");
        lblVTC_endDate.Text = model.VTC_EndDate.ToString("yyyy-MM-dd");
        //lblVTC_desc.Text = model.VTC_desc;
        divVTC_desc.InnerHtml = model.VTC_desc;

    }


    
    

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TextBox txtLoginName = null;
        TextBox txtRealName = null;
        TextBox txtMobile = null;
        TextBox txtEmail = null;
        DropDownList ddlIsBuyingAccount = null;

        HiddenField hidUserId = null;
        //HiddenField hidTrainingClassId = null;
        int failCount = 0;
        int successCount = 0;
        for (int i = 1; i <= 10; i++)
        {
            txtLoginName = (TextBox)this.FindControl("txtLoginName" + i);
            txtRealName = (TextBox)this.FindControl("txtUserRealName" + i);
            txtMobile = (TextBox)this.FindControl("txtMobile" + i);
            txtEmail = (TextBox)this.FindControl("txtEmail" + i);
            ddlIsBuyingAccount = (DropDownList)this.FindControl("ddlIsBuyingAccount" + i);
            if (txtLoginName.Text.Trim() == "" && txtRealName.Text.Trim() == "" && txtMobile.Text.Trim() == "" && txtEmail.Text.Trim() == "")
            {
                UserList.Add(null);
                continue;
            }

            hidUserId = (HiddenField)this.FindControl("hidUserId" + i);
            //hidTrainingClassId = (HiddenField)this.FindControl("hidTrainingClassId" + i);

            CreateUserList(hidUserId.Value, txtLoginName.Text.Trim(), txtRealName.Text.Trim(),
            txtEmail.Text.Trim(), txtMobile.Text.Trim());


        }
        UserModule userHelper = new UserModule();
        List<ReturnResult> resultList = userHelper.QuantityRegisterUser(UserList, this.SchoolID);
        ReturnResult result = null;
        for (int i = 0; i < resultList.Count; i++)
        {
            result = resultList[i];
            Label lblMsg = (Label)this.FindControl("lblMsg" + (i + 1));
            if (result.IsSuccess == true)
            {
                successCount += 1;
                lblMsg.Text = "成功!";
            }
            else if (result.ErrorMsg != "")
            {
                lblMsg.Text = result.ErrorMsg;
                failCount += 1;
            }
            else
            {

            }
        }
        if (successCount > 0 && failCount == 0)
        {
            JsHelper.AlertAndRedirect("添加成功！", "ViewTrainingClassList.aspx");
        }
    }

    private void CreateUserList(string userId, string txtLoginName, string txtRealName, string txtEmail, string txtMobile)
    {
        V_user user = new V_user();
        user.ID = int.Parse(userId);
        user.VU_loginname = txtLoginName;
        user.VU_Password = txtLoginName;
        user.VU_realname = txtRealName;
        user.VU_mobile = txtMobile;
        user.VU_email = txtEmail;
        //user.IsAvailable = IsAvailable;
        string[] trainingClassIdList = new string[1];
        //trainingClassIdList[0] = CurrentTraingClassId.ToString();
        //user.TrainingClassIdList = trainingClassIdList;
        //UserList.Add(user);

    }
    protected void btnRedirectList_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewUserInfoList.aspx");
    }
}


